package com.a2.appa3.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.model.CustomObserver;
import com.a2.appa3.model.network.HttpJsonUtils;
import com.a2.appa3.model.network.entity.req.Resbuildground;
import com.a2.appa3.util.WebSocketUtils2;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 退出登录的弹框
 */
public class ExportActivity extends Activity {

    TextView commitbt;
    TextView canclebt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_export);

        commitbt = findViewById(R.id.commitbt);
        canclebt = findViewById(R.id.canclebt);

        commitbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                SpUtils.clearusername(ExportActivity.this);
//                SpUtils.clearloginpass(ExportActivity.this);
                Info.Token = null;
                Info.user = null;
                //断开Socket
                WebSocketUtils2.getInstance().ondestoryWebsocket();
                //这里开始网络请求
                HttpJsonUtils.getHttpApiInstance()
                        .logout(HttpAdress.getuseAdress() + "api/m/logout", Info.Token)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new CustomObserver<Resbuildground>() {
                            @Override
                            protected void onCustomNext(Resbuildground resLogin) {
                                startActivity(new Intent(ExportActivity.this, LoginActivity.class));
                            }

                            @Override
                            protected void onSubscribeCustom(Disposable disposable) {
                            }

                            @Override
                            protected void onErrorCustom(Throwable e) {

                            }
                        });
                Toast.makeText(ExportActivity.this, "退出成功", Toast.LENGTH_SHORT).show();
                finish();

            }

        });
        canclebt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}
